﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfUsercontrol.Converts;
using WpfUsercontrol.Models.TimeLines;

namespace WpfUsercontrol.UserControls.TimeLines
{
    /// <summary>
    /// ClTimeLine.xaml 的交互逻辑
    /// </summary>
    public partial class ClTimeLine : UserControl, INotifyPropertyChanged
    {
        // 依赖属性：时间轴项集合
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<TimeLineItem>),
                typeof(ClTimeLine), new PropertyMetadata(null));

        public ObservableCollection<TimeLineItem> ItemsSource
        {
            get => (ObservableCollection<TimeLineItem>)GetValue(ItemsSourceProperty);
            set => SetValue(ItemsSourceProperty, value);
        }
        public ClTimeLine()
        {
            InitializeComponent();
            this.DataContext = this;
            this.Loaded += (s, e) => UpdateLastItemFlag();
        }
        private void UpdateLastItemFlag()
        {
            if (ItemsSource == null || ItemsSource.Count == 0) return;

            foreach (var item in ItemsSource)
                item.IsLastItem = false;

            ItemsSource.Last().IsLastItem = true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
